package com.intel.two.service.information.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intel.two.common.LayuiPageVo;
import com.intel.two.mapper.information.NewsMapper;
import com.intel.two.pojo.information.News;
import com.intel.two.service.information.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("/NewsService")
public class NewsServiceImpl implements NewsService {
    @Autowired
    private NewsMapper newsMapper;

    @Override
    public LayuiPageVo<News> newsPageData(Map<String, Object> params) {

        //mybatis 分页 插件的使用
        Integer page = Integer.parseInt(params.get("page")+"");
        Integer limit = Integer.parseInt(params.get("limit")+"");

        PageHelper.startPage(page,limit);
        List<News> newsList =  newsMapper.selectByParams(params);

        // mybatis 的分页数据结构
        PageInfo<News> newsPageInfo = new PageInfo<>(newsList);
        // 把mybatis 的分页数据结构中的 数据 封装到 layui的 分页数据结构中
        LayuiPageVo<News> newsLayuiPageVo = new LayuiPageVo<>();
        newsLayuiPageVo .setCode(0);
        newsLayuiPageVo .setMsg("学生分页列表数据");
        newsLayuiPageVo .setCount(newsPageInfo.getTotal());  // 总记录数
        newsLayuiPageVo .setData(newsPageInfo.getList());  // 分页列表数据

        return newsLayuiPageVo;
    }


    @Override
    public int deleteNews(Integer news_id) {
        return newsMapper.deleteNews(news_id);
    }

    @Override
    public int addNews(News news) {
        return newsMapper.addNews(news);
    }

    @Override
    public int updateNews(News news) {
        return newsMapper.updateNews(news);
    }

    @Override
    public int deleteNewsAll(Map<String, Object> map) {
        String str = (String) map.get("news_id");
        String[] strs = str.split("-");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < strs.length; i++) {
            list.add(Integer.parseInt(strs[i]));
            System.out.println(strs[i]);
        }
        return newsMapper.deleteNewsAll(list);
    }

}
